using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace Rensselaer_Event_Finder
{
    public partial class EventMDI : Form
    {
        #region Variables

        public Event curEvent;
        
        #endregion

        #region Public Methods

        public EventMDI()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load the event page with the specified event
        /// </summary>
        /// <param name="_event"></param>
        public void load_event(Event _event)
        {
            curEvent = _event;

            eventname.Text = _event.Name;
            startTime.Text = _event.StartTime.ToString("MM/dd/yyyy hh:mm");
            endTime.Text = _event.EndTime.ToString("MM/dd/yyyy hh:mm");
            location.Text = _event.Location.Name;
            
            //load category (possible more than 1)
            String categoryString = "";
            for (int i = 0; i < _event.Categories.Count; i++)
            {
                categoryString = categoryString + _event.Categories[i].Name + ", ";
            }
            if (categoryString != "")
            {
                eventTypeValue.Text = categoryString.Substring(0, categoryString.Length - 2);
            }
            else
            {
                eventTypeValue.Text = "N/A";
            }

            //load description
            if (_event.Description != null && _event.Description != "")
            {
                longDescript.Text = _event.Description;
            }
            else
            {
                longDescript.Text = "No description available.";
            }
            //load website
            if (_event.Website != null && _event.Website != "")
            {
                urlLabel.Enabled = true;
                urlLabel.Text = _event.Website;
            }
            else
            {
                urlLabel.Text = "N/A";
                urlLabel.Enabled = false;
            }
            //load price
            if (_event.Price != null && _event.Price != "")
            {
                price.Text = _event.Price;
            }
            else
            {
                price.Text = "N/A";
            }
            //load phone number
            if (_event.Phone != null && _event.Phone != "")
            {
                phoneVal.Text = _event.Phone;
            }
            else
            {
                phoneVal.Text = "N/A";
            }
            //load email
            if (_event.Email != null && _event.Email != "")
            {
                emailVal.Text = _event.Email;
            }
            else
            {
                emailVal.Text = "N/A";
            }
            if (_event.Image_url != "" && _event.Image_url != null)
            {
                primaryImage.Image = Globals.loadImageFromURL(_event.Image_url);
            }
            else
            {
                primaryImage.Image = Globals.loadImageFromURL(_event.Location.Image_url);
            }


            commentsBox.Items.Clear();
            for (int i = 0; i < curEvent.Comments.Count; i++)
            {
                commentsBox.Items.Add(curEvent.Comments[i].User.Username + ": " + curEvent.Comments[i].Text);
            }

            listOfGuests.Items.Clear();
            if (curEvent.Users.Count == 0)
            {
                listOfGuests.Items.Add("No Guests Attending...");
            }
            else
            {
                for (int i = 0; i < curEvent.Users.Count; i++)
                {
                    listOfGuests.Items.Add(curEvent.Users[i].Username);
                }
            }
            //load rsvp
            if (Globals.CurrentUser == null)
            {
                notAttending.Checked = true;
            }
            else
            {
                if (_event.Users.Contains(Globals.CurrentUser))
                {
                    attending.Checked = true;
                }
                else
                {
                    notAttending.Checked = true;
                }
            }
        }

        #endregion

        #region Private Methods

        private void homeBtn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.SelectMDIForm(MainForm.homeFrm);
        }

        private void location_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MainForm.venueFrm.load_venue(curEvent.Location);
            MainForm.SelectMDIForm(MainForm.venueFrm);
        }

        private void urlLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string url = ((LinkLabel)sender).Text;
            if (!(url.StartsWith("www.") || url.StartsWith("http")))
            {
                url = "www." + url;
            }

            System.Diagnostics.Process.Start(url);
        }

        #endregion

        private void addCommentBtn_Click(object sender, EventArgs e)
        {
            if (newCommentBox.Text != "")
            {
                if (Globals.CurrentUser == null)
                {
                    MessageBox.Show("Must login first", "Error");
                }
                else
                {
                    EventComment newCmt = new EventComment();
                    newCmt.User = Globals.CurrentUser;
                    newCmt.MyEvent = curEvent;
                    newCmt.Text = newCommentBox.Text;
                    curEvent.AddComment(newCmt);
                    Globals.getEventCommentService().save(newCmt);
                    commentsBox.Items.Add(Globals.CurrentUser.Username + ": " + newCommentBox.Text);
                    newCommentBox.Clear();
                }
            }
        }

        private void attending_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void notAttending_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void attending_Click(object sender, EventArgs e)
        {
            if (attending.Checked == true)
            {
                if (Globals.CurrentUser == null)
                {
                    MessageBox.Show("Must login first", "Error");
                }
                else
                {
                    if (!Globals.CurrentUser.Events.Contains(curEvent))
                    {
                        if (curEvent.Users.Count == 0)
                        {
                            listOfGuests.Items.Clear(); //clear no guests attending message
                        }
                        listOfGuests.Items.Add(Globals.CurrentUser.Username);
                        Application.DoEvents();
                        Globals.CurrentUser.Events.Add(curEvent);
                        Globals.getUserService().save(Globals.CurrentUser);
                    }
                }
            }
        }

        private void notAttending_Click(object sender, EventArgs e)
        {
            if (notAttending.Checked == true && Globals.CurrentUser != null)
            {
                if (Globals.CurrentUser.Events.Contains(curEvent))
                {
                    Globals.CurrentUser.Events.Remove(curEvent);
                    listOfGuests.Items.Remove(Globals.CurrentUser.Username);
                    if (curEvent.Users.Count == 0)
                    {
                        listOfGuests.Items.Add("No Guests Attending...");
                    }
                }
            }
        }

    }
}